package com.cheng.student.controller.servlet;

import com.cheng.student.beans.Teacher;
import com.cheng.student.service.TeacherServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "ChangeTeacherServlet", value = "/super/ChangeTeacherServlet")
public class ChangeTeacherServlet
        extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        String name = request.getParameter("name");
        String payrollNumber = request.getParameter("payrollNumber");
        String newName = request.getParameter("newName");
        String newPassword = request.getParameter("newPassword");

        //校验
        if(name == null || payrollNumber == null || "".equals(name) || "".equals(name))
        {
            response.getWriter().write("请核对");
            return;
        }

        if((newName == null || "".equals(newName))
                && (newPassword == null || "".equals(newPassword))
        )
        {
            response.getWriter().write("请核对");
            return;
        }

        //通过
        if("".equals(newName))
        {
            newName = null;
        }
        if("".equals(newPassword))
        {
            newPassword = null;
        }
        Teacher newTeacher = new Teacher();
        newTeacher.setName(newName);
        newTeacher.setPassword(newPassword);
        try
        {
            new TeacherServiceImpl().flushTeacherByName(name,payrollNumber,newTeacher);
        }
        catch (Exception e)
        {
            response.getWriter().write("修改失败");
            return;
        }

        response.getWriter().write("修改成功");
    }
}
